package generic.genetic;

import java.util.Random;

public abstract class Gene<GeneType,PhenoType> {

	protected GeneType[] genotype;
	
	protected int length;
	
	public abstract PhenoType phenotype();
	public abstract GeneType randomGeneTypeValue();
	public abstract Gene getCopy();
	
	public void randomInit(){
		for (int i=0;i<length;i++) genotype[i] = randomGeneTypeValue();		
	}
	
	public void mutation(double mutation_rate){
		Random r = new Random();
		for (int i=0;i<genotype.length;i++){
			if (r.nextFloat() < mutation_rate){
				//FIXME Avoid to use repeated values
				genotype[i] = randomGeneTypeValue();
			}
		}
	}
		
	public GeneType getGenotype(int i) {
		return genotype[i];
	}
	
	public void setGenotype(int i, GeneType value) {
		this.genotype[i] = value;
	}
	
	public int getLength() {
		return length;
	}
	
	public void cross(Gene gene, int cp) {
		for (int i=cp+1;i<length;i++){
			Object tmp = genotype[i];
			genotype[i] = (GeneType) gene.getGenotype(i);
			gene.setGenotype(i,tmp);
		}		
	}
	

	
}
